package com.hnzbxy.api.biz.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hnzbxy.api.biz.converter.ClassAdvisorConverter;
import com.hnzbxy.api.biz.entity.ClassAdvisor;
import com.hnzbxy.api.biz.mapper.ClassAdvisorMapper;
import com.hnzbxy.api.biz.service.ClassAdvisorService;
import com.hnzbxy.api.biz.vo.ClassAdvisorVO;
import com.hnzbxy.api.common.exception.ServiceException;
import com.hnzbxy.api.system.vo.PageVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;

/**
 * 班主任业务逻辑实现
 * @Author xiaoge
 * @Date 2020/3/10 11:56
 * @Version 1.0
 **/
@Service
public class ClassAdvisorServiceImpl implements ClassAdvisorService {

    @Autowired
    private ClassAdvisorMapper classAdvisorMapper;

    /**
     * 分页+条件查询班主任列表（参考UserService的Example风格）
     */
    @Override
    public PageVO<ClassAdvisorVO> findClassAdvisorList(Integer pageNum, Integer pageSize, ClassAdvisorVO classAdvisorVO) {
        // 1. 开启分页
        PageHelper.startPage(pageNum, pageSize);

        // 2. 构建Example查询条件（与UserService保持一致）
        Example example = new Example(ClassAdvisor.class);
        Example.Criteria criteria = example.createCriteria();

        // 3. 从VO中获取查询条件，非空才添加（与前端查询表单字段对应）
        String name = classAdvisorVO.getName();
        String gender = classAdvisorVO.getGender(); // 性别：1=男，2=女
        String phone = classAdvisorVO.getPhone();
        String email = classAdvisorVO.getEmail();

        // 姓名模糊查询
        if (!StringUtils.isEmpty(name)) {
            criteria.andLike("name", "%" + name + "%");
        }
        // 性别精确查询
        if (!StringUtils.isEmpty(gender)) {
            criteria.andEqualTo("gender", gender);
        }
        // 手机号模糊查询
        if (!StringUtils.isEmpty(phone)) {
            criteria.andLike("phone", "%" + phone + "%");
        }
        // 邮箱模糊查询
        if (!StringUtils.isEmpty(email)) {
            criteria.andLike("email", "%" + email + "%");
        }

        // 4. 执行查询（使用Mapper的selectByExample方法，无需额外XML）
        List<ClassAdvisor> classAdvisorList = classAdvisorMapper.selectByExample(example);

        // 5. 实体列表转VO列表（复用Converter）
        List<ClassAdvisorVO> voList = ClassAdvisorConverter.converterToListVO(classAdvisorList);

        // 6. 封装分页结果（与UserService返回格式一致）
        PageInfo<ClassAdvisor> pageInfo = new PageInfo<>(classAdvisorList);
        return new PageVO<>(pageInfo.getTotal(), voList);
    }

    /**
     * 按条件查询所有班主任（用于Excel导出，无分页）
     */
    @Override
    public List<ClassAdvisor> findAllByCondition(ClassAdvisorVO classAdvisorVO) {
        // 复用查询条件逻辑
        Example example = new Example(ClassAdvisor.class);
        Example.Criteria criteria = example.createCriteria();

        String name = classAdvisorVO.getName();
        String gender = classAdvisorVO.getGender();
        String phone = classAdvisorVO.getPhone();
        String email = classAdvisorVO.getEmail();

        if (!StringUtils.isEmpty(name)) {
            criteria.andLike("name", "%" + name + "%");
        }
        if (!StringUtils.isEmpty(gender)) {
            criteria.andEqualTo("gender", gender);
        }
        if (!StringUtils.isEmpty(phone)) {
            criteria.andLike("phone", "%" + phone + "%");
        }
        if (!StringUtils.isEmpty(email)) {
            criteria.andLike("email", "%" + email + "%");
        }

        return classAdvisorMapper.selectByExample(example);
    }

    /**
     * 获取所有班主任列表
     */
    @Override
    public List<ClassAdvisor> findAll() {
        List<ClassAdvisor> list = classAdvisorMapper.selectAll();
        return CollectionUtils.isEmpty(list) ? new ArrayList<>() : list;
    }

    /**
     * 新增班主任
     */
    @Override
    public ClassAdvisor add(ClassAdvisorVO classAdvisorVO) {
        // VO转实体
        ClassAdvisor classAdvisor = ClassAdvisorConverter.converterToEntity(classAdvisorVO);
        // 插入数据库
        int insert = classAdvisorMapper.insert(classAdvisor);
        if (insert != 1) {
            throw new ServiceException("新增班主任失败");
        }
        return classAdvisor;
    }

    /**
     * 删除班主任
     */
    @Override
    public void delete(Long id) {
        // 先查询记录是否存在
        ClassAdvisor classAdvisor = classAdvisorMapper.selectByPrimaryKey(id);
        if (classAdvisor == null) {
            throw new ServiceException("要删除的班主任不存在");
        }
        // 执行删除
        int delete = classAdvisorMapper.deleteByPrimaryKey(id);
        if (delete != 1) {
            throw new ServiceException("删除班主任失败");
        }
    }

    /**
     * 获取班主任详情（编辑用）
     */
    @Override
    public ClassAdvisorVO edit(Long id) {
        // 查询记录
        ClassAdvisor classAdvisor = classAdvisorMapper.selectByPrimaryKey(id);
        if (classAdvisor == null) {
            throw new ServiceException("要编辑的班主任不存在");
        }
        // 实体转VO
        return ClassAdvisorConverter.converterToVO(classAdvisor);
    }

    /**
     * 更新班主任信息
     */
    @Override
    public void update(Long id, ClassAdvisorVO classAdvisorVO) {
        // 先查询记录是否存在
        ClassAdvisor dbClassAdvisor = classAdvisorMapper.selectByPrimaryKey(id);
        if (dbClassAdvisor == null) {
            throw new ServiceException("要更新的班主任不存在");
        }
        // VO转实体，并设置ID（确保更新指定记录）
        ClassAdvisor classAdvisor = ClassAdvisorConverter.converterToEntity(classAdvisorVO);
        classAdvisor.setId(id);
        // 执行更新（选择性更新，只更新非null字段）
        int update = classAdvisorMapper.updateByPrimaryKeySelective(classAdvisor);
        if (update != 1) {
            throw new ServiceException("更新班主任信息失败");
        }
    }

}